#include "Session.h"

//inicializo la instancia unica
Session* Session::instance = NULL;

Session::Session(){

	//inicializo el diccionario de datos
	attributes = new Diccionario();
}

Session* Session::getInstance(){

	if(instance == NULL)
		instance = new Session();

	return instance;
}

void Session::setPlayerOneLogin(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(PLAYER_ONE_LOGIN_ATT, *((TextBox*)widget)->getText());
}

string* Session::getPlayerOneLogin(){
	return getAttribute(PLAYER_ONE_LOGIN_ATT);
}

void Session::setPlayerTwoLogin(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(PLAYER_TWO_LOGIN_ATT, *((TextBox*)widget)->getText());
}

string* Session::getPlayerTwoLogin(){
	return getAttribute(PLAYER_TWO_LOGIN_ATT);
}

void Session::setPlayerOnePass(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(PLAYER_ONE_PASSW_ATT, *((TextBox*)widget)->getText());
}

string* Session::getPlayerOnePass(){
	return getAttribute(PLAYER_ONE_PASSW_ATT);
}

void Session::setPlayerTwoPass(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(PLAYER_TWO_PASSW_ATT, *((TextBox*)widget)->getText());
}

string* Session::getPlayerTwoPass(){
	return getAttribute(PLAYER_TWO_PASSW_ATT);
}

void Session::setNewPlayerLogin(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(REGISTER_LOGIN_ATT, *((TextBox*)widget)->getText());
}

string* Session::getNewPlayerLogin(){
	return getAttribute(REGISTER_LOGIN_ATT);
}

void Session::setNewPlayerPass(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(REGISTER_PASSW_ATT, *((TextBox*)widget)->getText());
}

string* Session::getNewPlayerPass(){
	return getAttribute(REGISTER_PASSW_ATT);
}

void Session::setNewPlayerPassConf(Widget* widget, const SDL_Event& event){
	Session::getInstance()->setAttribute(REGISTER_PASSW_CONF_ATT, *((TextBox*)widget)->getText());
}

string* Session::getNewPlayerPassConf(){
	return getAttribute(REGISTER_PASSW_CONF_ATT);
}


void Session::setAttribute(const string& attName, const string& attValue){
	this->attributes->put(attName, attValue);
}

string* Session::getAttribute(const string& attName){
	return this->attributes->get(attName);
}

void Session::clear(){

	//si no fue eliminado, elimino el diccionario de atributos
	if(attributes != NULL){
		delete attributes;
		attributes = NULL;
	}

	//vuelvo a crearlo
	this->attributes = new Diccionario();

};

void Session::destroy(){

	//vacio la sesion
	clear();

	//si no fue eliminada aun, elimino la instancia
	if(instance != NULL){	
		delete instance;
		instance = NULL;
	}
}

Session::~Session(){
	destroy();
}
